/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.AnggotaBean;

/**
 *
 * @author elfatah
 */
public class DBAnggota extends DBClass{

   public void UpdateAnggota(AnggotaBean bb, String id) throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("UPDATE `ekud`.`anggota` SET `NAMA` =?,`ALAMAT` =?,`TANGGAL_LAHIR` =?,`ID_KELAMIN` =?,`TELP`=?,`PEKERJAAN` =?,`ID_AGAMA` =? WHERE `anggota`.`ANGGOTA_ID` =?");
        //set values to prepared statement object by getting values from bean object
        pstmt.setString(1, bb.getNama());
        pstmt.setString(2, bb.getAlamat());
        pstmt.setString(3, bb.getTanggalLahir());
        pstmt.setString(4, bb.getKelamin());
        pstmt.setString(5, bb.getTelp());
        pstmt.setString(6, bb.getPekerjaan());
        pstmt.setString(7, bb.getAgama());
        pstmt.setString(8, id);
        pstmt.executeUpdate();

    }
     public void deleteAnggota(String uname) throws SQLException, ClassNotFoundException {
        //here we will write code to get a single record from database
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("delete from anggota where ANGGOTA_ID=?");
        pstmt.setString(1, uname);
        pstmt.executeUpdate();
    }
 public AnggotaBean getAnggotaDetails(String id) throws SQLException, ClassNotFoundException {
        //here we will write code to get a single record from database
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("SELECT an.`ANGGOTA_ID`, an.`NAMA`, an.`ALAMAT`, an.`TANGGAL_LAHIR`,ke.KELAMIN_ID, ke.NAMA, an.`TELP`, an.`PEKERJAAN`,ag.AGAMA_ID ,ag.NAMA FROM `anggota` an, agama ag, kelamin ke where an.`ID_AGAMA` = ag.AGAMA_ID and an.`ID_KELAMIN` = ke.KELAMIN_ID and `ANGGOTA_ID` =?");
        pstmt.setString(1, id);
        ResultSet rs = pstmt.executeQuery();

        AnggotaBean ubean = new AnggotaBean();
        while (rs.next()) {
            ubean.setAnggotaId(rs.getString(1));
            ubean.setNama(rs.getString(2));
            ubean.setAlamat(rs.getString(3));
            ubean.setTanggalLahir(rs.getString(4));
            ubean.setKelamin(rs.getString(5));
            ubean.setNamaKelamin(rs.getString(6));
            ubean.setTelp(rs.getString(7));
            ubean.setPekerjaan(rs.getString(8));
            ubean.setAgama(rs.getString(9));
            ubean.setNamaAgama(rs.getString(10));
        }
        ubean.setAction("Update");
        return ubean;
    }
public List getAnggotaDetails() throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("SELECT an.`ANGGOTA_ID`, an.`NAMA`, an.`ALAMAT`, an.`TANGGAL_LAHIR`, ke.NAMA, an.`TELP`, an.`PEKERJAAN`, ag.NAMA,an.IURAN FROM `anggota` an, agama ag, kelamin ke where an.`ID_AGAMA` = ag.AGAMA_ID and an.`ID_KELAMIN` = ke.KELAMIN_ID");
        ResultSet rs = pstmt.executeQuery();
        List list = new ArrayList();
        while (rs.next()) {
            AnggotaBean ubean = new AnggotaBean();
            ubean.setAnggotaId(rs.getString(1));
            ubean.setNama(rs.getString(2));
            ubean.setAlamat(rs.getString(3));
            ubean.setTanggalLahir(rs.getString(4));
            ubean.setKelamin(rs.getString(5));
            ubean.setTelp(rs.getString(6));
            ubean.setPekerjaan(rs.getString(7));
            ubean.setAgama(rs.getString(8));
            ubean.setIuran(rs.getString(9));

            list.add(ubean);

        }
        return list;
    }

   public void insertAnggota(AnggotaBean bb) throws SQLException, ClassNotFoundException {
        try {
            Connection con = createConnection();
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO `ekud`.`anggota` (`NAMA` ,`ALAMAT` ,`TANGGAL_LAHIR` ,`ID_KELAMIN` ,`TELP` ,`PEKERJAAN` ,`ID_AGAMA`,`IURAN`)VALUES (?,?,?,?,?,?,?,?)");
            //set values to prepared statement object by getting values from bean object
           // pstmt.setString(1, bb.getAnggotaId());
            pstmt.setString(1, bb.getNama());
            pstmt.setString(2, bb.getAlamat());
            pstmt.setString(3, bb.getTanggalLahir());
            pstmt.setString(4, bb.getKelamin());
            pstmt.setString(5, bb.getTelp());
            pstmt.setString(6, bb.getPekerjaan());

            pstmt.setString(7, bb.getAgama());
            pstmt.setString(8, bb.getIuran());
            pstmt.executeUpdate();
            System.out.println("testing");
           
        } catch (Exception e) {
            System.out.println(e);
            
        }
    }
    
}
